package server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

public class RecordDatabas implements Serializable {
	private static final long serialVersionUID = -8050972528266347173L;
	private ArrayList<Record> records;

	public RecordDatabas() {
		records = new ArrayList<Record>();
	}

	public boolean addRecord(User user, Record record) {
		boolean success = false;
		if (user.getGroup() == Group.Doctor)
			success = records.add(record);
		if (success) {
			save();
			return success;
		}
		return false;
	}

	public boolean deleteRecord(User user, long ssn, long timestamp) {
		if (user.getGroup() == Group.Government) {
			Record rtd = null;
			for (Record record : records) {
				if (record.getPatient() == ssn
						&& record.getTimestamp() == timestamp) {
					rtd = record;
					break;
				}
			}
			boolean returner = records.remove(rtd);
			save();
			return returner;
		}
		return false;
	}

	public boolean writeToRecord(User user, long ssn, long timestamp,
			int division, long nurse, String notes) {
		if (user.getGroup() == Group.Doctor || user.getGroup() == Group.Nurse) {
			for (Record record : records) {
				if (record.getPatient() == ssn
						&& record.getTimestamp() == timestamp) {
					if (user.getSsn() == record.getDoctor()
							|| user.getSsn() == record.getNurse()) {
						record.setDivision(division);
						record.setNurse(nurse);
						record.writeToRecord(notes + " (" + user.getSsn() + ")");
						save();
						return true;
					}
				}
			}
		}
		return false;
	}

	public String listRecords(User user) {
		StringBuilder stringBuilder = new StringBuilder();
		if (user.getGroup() == Group.Government) {
			for (Record record : records) {
				stringBuilder
						.append(String.format("%010d", record.getPatient())
								+ ";" + record.getTimestamp() + "\n");
			}
		} else if (user.getGroup() == Group.Doctor
				|| user.getGroup() == Group.Nurse) {
			for (Record record : records) {
				if (user.getDivision() == record.getDivision()
						|| user.getSsn() == record.getDoctor()
						|| user.getSsn() == record.getNurse())
					stringBuilder.append(String.format("%010d",
							record.getPatient())
							+ ";" + record.getTimestamp() + "\n");
			}
		} else if (user.getGroup() == Group.Patient) {
			for (Record record : records) {
				if (user.getSsn() == record.getPatient())
					stringBuilder.append(String.format("%010d",
							record.getPatient())
							+ ";" + record.getTimestamp() + "\n");
			}
		}
		return stringBuilder.toString();
	}

	public Record readRecord(User user, long ssn, long timestamp) {
		if (user.getGroup() == Group.Government) {
			for (Record record : records) {
				if (record.getPatient() == ssn
						&& record.getTimestamp() == timestamp)
					return record;
			}
		} else if (user.getGroup() == Group.Doctor
				|| user.getGroup() == Group.Nurse) {
			for (Record record : records) {
				if (record.getPatient() == ssn
						&& record.getTimestamp() == timestamp)
					if (user.getSsn() == record.getDoctor()
							|| user.getSsn() == record.getNurse()
							|| user.getDivision() == record.getDivision())
						return record;
			}
		} else if (user.getGroup() == Group.Patient) {
			for (Record record : records) {
				if (record.getPatient() == ssn
						&& record.getTimestamp() == timestamp)
					if (user.getSsn() == record.getPatient())
						return record;
			}
		}
		return null;
	}

	private synchronized void save() {
		String filename = "data.txt";
		try {
			ObjectOutputStream out = new ObjectOutputStream(
					new FileOutputStream(new File(filename)));
			out.writeObject(this);
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static RecordDatabas load() {
		String filename = "data.txt";
		RecordDatabas data = null;
		try {
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(
					new File(filename)));
			data = (RecordDatabas) in.readObject();
			in.close();
		} catch (FileNotFoundException e) {
			System.out.println("No database found");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		if (data != null)
			return data;
		else
			return new RecordDatabas();
	}
}
